var _eiinfo;
var _regions = new Array()
_regions[0] = "ef_region_result";
_regions[1] = "ef_region_detail";

efform_onload = function() {
	_eiinfo = new _getEi();
	$('#ef_region_detail').hide();
	hideAndShowSubButtons(false);
	queryMain();
}; 
/**
 * 查询
 */
function queryMain() {
	efgrid.submitInqu("ef_grid_result", "ZM", "ZMCC30", "query");
};

/**
 * 按钮显示/隐藏
 */
var hideAndShowSubButtons = function(status) {
	if (!status) {
		efbutton.setButtonStatus("confirm", false);
		efbutton.setButtonStatus("cancel", false);
	} else {
		efbutton.setButtonStatus("confirm", true);
		efbutton.setButtonStatus("cancel", true);
	}
};

/**
 * 复核
 */
button_recheck_onclick = function() {

	var grid = efgrid.getGridObject("ef_grid_result");
	var rows = grid.getCheckedRows();
	var rowLength = rows.length;
	if (rowLength != 1) {
		EFAlert("请选择一条生产问题！");
		return;
	}

	var fdRecordId = grid.getCellValue(rows[0], 0, TYPE_DATA, true);
	var fdRecordHandleId = grid.getCellValue(rows[0], 1, TYPE_DATA, true);
	var fdRecordModifyId = grid.getCellValue(rows[0], 2, TYPE_DATA, true);
	var fdIsRepeat = grid.getCellValue(rows[0], 3, TYPE_DATA, true);

	var _info = new EiInfo();
	_info.set("fdRecordId", fdRecordId);
	_info.set("fdRecordHandleId", fdRecordHandleId);
	_info.set("fdRecordModifyId", fdRecordModifyId);
	_info.set("fdIsRepeat", fdIsRepeat);
	EiCommunicator.send("ZMCC30", "queryForDetail", _info, ajax_recheckShow_callback);
};
var ajax_recheckShow_callback= {
		onSuccess: function(eiInfo) {
			//将查回来的数据填充到ef_region_previewadd表中，为数据新增做好准备
			var block = eiInfo.getBlock("result");  
			if(block.getRows()[0]!=null)
			{
				$('#detail-0-fdRecordId').val(block.getCell(0, "fdRecordId")); 
				$('#detail-0-fdRecordHandleId').val(block.getCell(0, "fdRecordHandleId")); 
				$('#detail-0-fdRecordModifyId').val(block.getCell(0, "fdRecordModifyId")); 
				$('#detail-0-fdIsRepeat').val(block.getCell(0, "fdIsRepeat"));				
				$('#detail-0-fdIsPenalty').val(block.getCell(0, "fdIsPenalty"));
				$('#detail-0-fdIsPenalty').attr("readonly","readonly");
				$('#detail-0-fdIsPenalty').attr("disabled","disabled");
				// 是否已交罚款
				if(block.getCell(0, "fdIsPenalty") == "否") {
					$('#detail-0-fdIsSubmit').val("无");
					$('#detail-0-fdIsSubmit').attr("readonly","readonly");
					$('#detail-0-fdIsSubmit').attr("disabled","disabled");
				} else {
//					$('#detail-0-fdIsPenalty').val("是");
					$('#detail-0-fdIsSubmit').val("否");
					$('#detail-0-fdIsSubmit').removeAttr("readonly");
					$('#detail-0-fdIsSubmit').removeAttr("disabled");
				} 
				
				$('#detail-0-fdCreateTime').val(getNowDateTime()); 
				$('#detail-0-fdRecheckDate').val(getNowDate());  
				$('#detail-0-fdRecheckerDepartmentId').val(block.getCell(0, "fdRecheckerDepartmentId")); 
				$('#detail-0-fdRecheckerDepartment').val(block.getCell(0, "fdRecheckerDepartment")); 
				$('#detail-0-fdRecheckPeopleId').val(block.getCell(0, "fdRecheckPeopleId")); 
				$('#detail-0-fdRecheckPeople').val(block.getCell(0, "fdRecheckPeople")); 
				
				hideAllRegions(_regions);
				$('#ef_region_detail').show();  
				hideAndShowSubButtons(true);
			}
		}
	};
/**
 * 复核后确定
 */
button_confirm_onclick = function() {

	var isCheck = checkRecheck();
	if (isCheck == 0) {
		return;
	}

	if (efvalidateDiv("ef_region_detail")) {
		var info = getDetail("ef_region_detail", "detail");
		if (null != info) {
			EiCommunicator.send("ZMCC30", "recheck", info, ajax_callback);
		}
	}
};

/**
 * 回调
 */
var ajax_callback = {
	onSuccess : function(eiInfo) {
		var grid = efgrid.getGridObject("ef_grid_result");
		grid.refresh(eiInfo);// 刷新主表grid
		hideAllRegions(_regions)
		$('#ef_region_result').show();
		clearAfterSolve("ef_region_detail");
		queryMain();
	},
	onFail : function(eMsg) {
		EFAlert("反馈失败，原因：" + eMsg);
	}
};

/**
 * 复查页面 关闭
 */
button_cancel_onclick = function() {
	hideAllRegions(_regions);
	$('#ef_region_result').show();
};
/**
 * tab切换
 * @param currentIndex
 * @param index
 */
function fundiv(currentIndex, index) {
	currentIndexTemp = currentIndex;
	indexTemp = index;
	if (index == 0) {

	} else if (index == 1) {
		var frame = frames[0];

		frame.src = "DispatchAction.do?efFormEname=ZMCC31";
		efform.setStatus(0, "正在加载...");
	}
};
/**
 * 操作完成后清空该region内的各数据项的值
 */
var clearAfterSolve = function(region) {
	var temp;
	// 遍历INPUT
	$('#' + region).find("input").each(function() {
		temp = $(this).attr("id");
		$('#' + temp).val("");
	});
	// 遍历SELECT
	$('#' + region).find("select").each(function() {
		temp = $(this).attr("id");
		$('#' + temp).val("");
	});
	// 遍历TEXTAREA
	$('#' + region).find("textarea").each(function() {
		temp = $(this).attr("id");
		$('#' + temp).val("");
	});
};

/**
 * 复查——输入项校验
 */
function checkRecheck() {
	var fdCondition = $("#detail-0-fdCondition").val().trim(); // 复查情况描述
	var fdRedoSuggestion = $("#detail-0-fdRedoSuggestion").val().trim(); // 重新整改意见

	if (fdCondition.getBytes() > 100) {
		SCZXAlert("复查情况描述不能超过100个字符！");
		return false;
	}
	if (fdRedoSuggestion.getBytes() > 100) {
		SCZXAlert("重新整改意见备注不能超过100个字符！");
		return false;
	}
	return true;
};

String.prototype.getBytes = function() {
	var cArr = this.match(/[^\x00-\xff]/ig);
	return this.length + (cArr == null ? 0 : cArr.length);

};

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};